package com.hooker.plugins;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.BaseBundle;
import android.os.Bundle;
import android.os.Parcel;

import com.hooker.base.BaseXposedLoader;
import com.hooker.common.Reflect;
import com.hooker.common.Reflection;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class GPAdActvitiyEntry extends BaseXposedLoader {
    public GPAdActvitiyEntry(Context context, XC_LoadPackage.LoadPackageParam loadParams) {
        super(context, loadParams);
    }

    @Override
    public void hook(Object... calls) {
        log("hook~~");
        hookMethod(Activity.class, "onCreate", Bundle.class, new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                log("onCreate "+param.thisObject.getClass().getName());
                if("com.google.android.finsky.activities.MarketDeepLinkHandlerActivity".equals(param.thisObject.getClass().getName())){
                    for (StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {
                        log("onCreate " +stackTraceElement);
                    }
                }

                if("com.google.android.gms.ads.AdActivity".equals(param.thisObject.getClass().getName())){
                    for (StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {
                        log("onCreate " +stackTraceElement);
                    }

                    Intent intent = ((Activity) param.thisObject).getIntent();
                    Bundle mExtras = (Bundle)Reflection.get(intent, Intent.class.getName(),"mExtras");
                    log(" mExtras:"+mExtras);

                    Object mParcelledData = null;
                    mParcelledData = Reflection.get(mExtras, Bundle.class.getSuperclass().getName(), "mParcelledData");
                    log(" mParcelledData:"+mParcelledData);
                    Parcel parcel;

                    Object mMap = null;
                        mMap = Reflection.get(mExtras, Bundle.class.getSuperclass().getName(), "mMap");
                        log(" mMap:"+mMap);
                }
            }

            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
            }
        });

        try {
            XposedBridge.hookAllMethods(loadParams.classLoader.loadClass("android.app.ContextImpl"), "startActivities", new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    super.beforeHookedMethod(param);
                    log("startActivities:"+param.args[0]);
                }

                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                }
            });

            XposedBridge.hookAllMethods(loadParams.classLoader.loadClass("android.app.ContextImpl"), "startActivity", new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    super.beforeHookedMethod(param);
                    log("startActivity:"+param.args[0]);
                }

                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                }
            });
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
